//
//  NSString+Check.m
//  Association
//
//  Created by 一览iOS on 14-6-19.
//  Copyright (c) 2014年 job1001. All rights reserved.
//

#import "NSString+Check.h"

@implementation NSString (Check)

//手机号码判断
- (BOOL)isMobileNumValid
{
    NSString * PHS = @"1[3|4|5|7|8|9][0-9]{9}";
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
    BOOL result = [regextestct evaluateWithObject:self];
    return result;
}

//固定电话判断
- (BOOL)checkNumber{
    
    NSString *strNum = @"^(0\\d{2,3}-{0,1}\\d{7,8})$";
    
    NSPredicate *checktest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum];
    return [checktest evaluateWithObject:self];
}
//Email校验
- (BOOL)validateEmail{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

// 正则匹配
+ (NSArray *)matchString:(NSString *)string toRegexString:(NSString *)regexStr {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray * matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    //match: 所有匹配到的字符,根据() 包含级
    NSMutableArray *array = [NSMutableArray array];
    for (NSTextCheckingResult *match in matches) {
        for (int i = 0; i < [match numberOfRanges]; i++) {
            //以正则中的(),划分成不同的匹配部分
            NSString *component = [string substringWithRange:[match rangeAtIndex:i]];
            [array addObject:component];
        }
    }
    return array;
}

// 正则匹配结果
- (BOOL)isValidToRegexString:(NSString *)regexStr {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexStr];
    return [predicate evaluateWithObject:self];
}

/**
 *  空字符串处理
 */
-(BOOL)isNull
{
    // 判断是否为空串
    if ([self isEqual:[NSNull null]]) {
        return YES;
    }
    else if ([self isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else if (self == nil){
        return YES;
    }
    else if ([self isEqualToString:@""]){
        return YES;
    }
    return NO;
}

+ (NSString *)unEmptyString:(NSString *)string {
    if (StringIsEmpty(string) || [string isEqualToString:@"(null)"]) {
        return @"";
    }
    return string;
}

+ (NSString *)changeNullOrNil:(NSString *)string;
{
    if ([string isEqual:[NSNull null]] || string == nil  || string.length == 0) {
        return @"0";
    }else{
        return string;
    }
}

+ (NSString *)changeNull:(NSString *)string
{
    if ([string isEqual:[NSNull null]] || string == nil) {
        return @"";
    }
    return string;
}

+ (NSString *)httpToHttps:(NSString *)string
{
    return string;
    
    if ([string isKindOfClass:[NSString class]]) {
        NSString *resultStr;
        //只替换开头
        if ([string hasPrefix:@"http"] && ![string hasPrefix:@"https"]) {
            resultStr = [string substringWithRange:NSMakeRange(4, string.length-4)];
            resultStr = [NSString stringWithFormat:@"https%@",resultStr];
            
            return resultStr;
        }
        return string;
    }
    return @"";
}

+ (BOOL)domainNameConstantString:(NSString *)urlStr
{
    for (NSString *string in [Manager shareMgr].getUrlArr) {
        if ([urlStr containsString:string]) {
            return YES;
        }
    }
    return NO;
}

+ (NSString *)changeLinkUrl:(NSString *)url
{
    if (StringIsNotEmpty(url))
    {
        NSString *timeNow = [NSString stringWithFormat:@"%.f",[[NSDate date] timeIntervalSince1970]];
        NSString *personIdStr = [Manager getUserInfo].userId_;
        if(StringIsEmpty(personIdStr)){
            personIdStr = @"";
        }
        
        NSString *md5_1 = [MD5 getMD5:[NSString stringWithFormat:@"%@%@",personIdStr,timeNow]];
        
        NSString *mD5CodeStr = @"1qaz2wsx1001";
        
        NSString *cksMD5Str = [MD5 getMD5:[NSString stringWithFormat:@"%@%@",md5_1, mD5CodeStr]];
        
        if (![url containsString:@"appflag"])
        {//子页面没有包含appflag，则拼接appflag=1002
            if([url containsString:@"?"]){
                url = [NSString stringWithFormat:@"%@&appflag=1002",url];
            }else{
                url = [NSString stringWithFormat:@"%@?appflag=1002",url];
            }
        }
        
        NSString *relUrl = [NSString stringWithFormat:@"%@&personid=%@&time=%@&version=%@&cks=%@",url,personIdStr,timeNow,Request_Version,cksMD5Str];
        
        return relUrl;
    }
    return nil;
}


#pragma mark - h5链接登录一览账号校验后跳转处理
+ (NSString *)checkAndLoginELanwAccountWithURL:(NSString *)url
{
    if (StringIsNotEmpty(url))
    {
        if (![url containsString:@"appflag"])
        {//子页面没有包含appflag，则拼接appflag=1002   (ios标识符)
            if([url containsString:@"?"]){
                url = [NSString stringWithFormat:@"%@&appflag=1002",url];
            }else{
                url = [NSString stringWithFormat:@"%@?appflag=1002",url];
            }
        }
        url = [NSString stringWithFormat:@"%@&checklogin=havedone",url];
        NSString *timeNow = [NSString stringWithFormat:@"%.f",[[NSDate date] timeIntervalSince1970]];
        NSString *personIdStr = [Manager getUserInfo].userId_;
        if(StringIsEmpty(personIdStr) || [url containsString:@"http://m.yl1001.com/general/login?pid="]){
            return url;
        }
        NSString *mD5CodeStr = @"1qaz2wsx1001";
        NSString *md5_1 = [MD5 getMD5:[NSString stringWithFormat:@"%@%@",personIdStr,timeNow]];
        NSString *cksMD5Str = [MD5 getMD5:[NSString stringWithFormat:@"%@%@",md5_1, mD5CodeStr]];
        NSString *encodedString = [url URLEncodedForString];

        NSString *relUrl = [NSString stringWithFormat:@"http://m.yl1001.com/general/login?pid=%@&time=%@&cks=%@&jumpurl=%@", personIdStr, timeNow, cksMD5Str, encodedString];
        return relUrl;
    }

    return nil;
}

+ (BOOL)isMoneyNumber:(NSString *)moneyNum
{
    NSString * PHS = @"^[1-9]\\d*\\.\\d*$|^0\\.\\d*[1-9]\\d*$|^[1-9]\\d*$";
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
    
    if ([regextestct evaluateWithObject:moneyNum] == YES) {
        return YES;
    }else{
        return NO;
    }
}


- (BOOL)isNumberString
{
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:self]) {
        return YES;
    }
    return NO;
}


@end
